package com.sbl.bajw.pc.service;

import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sbl.bajw.common.MyBatisDao;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.mapper.MsgBoxDao;
import com.sbl.bajw.pc.model.MsgBoxModel;

@Service
public class MsgBoxService {
	
	@Resource
	private MsgBoxDao dao;
	
	@Resource
	private MyBatisDao gridDao;
	
	public void insert(MsgBoxModel model){
		String useridsStr = RequestUnit.getString("userids");
		String[] userids = useridsStr.split(",");
		model.setCreater(SessionUnit.getUserId());
		for(int i=0;i<userids.length;i++){
			String userid = userids[i];
			model.setUserid(userid);
			dao.insert(model);
		}
	}
	
	public String getList(){
		String title = RequestUnit.getString("title");
		String content = RequestUnit.getString("content");
		String creater = RequestUnit.getString("creater");
		String tuser = RequestUnit.getString("tuser");
		String readed = RequestUnit.getString("readed");
		String startDate = RequestUnit.getString("startDate");
		String stopDate = RequestUnit.getString("stopDate");
		
		HashMap<String,Object> parm = new HashMap<String, Object>();
		if(title != null && !title.equals("")){
			parm.put("title", "%"+title+"%");
		}
		
		if(content != null && !content.equals("")){
			parm.put("content", "%"+content+"%");
		}
		
		if(creater != null && !creater.equals("")){
			parm.put("creater", "%"+creater+"%");
		}
		
		if(tuser != null && !tuser.equals("")){
			parm.put("tuser", "%"+tuser+"%");
		}
		
		parm.put("readed", readed);
		parm.put("startDate", startDate);
		parm.put("stopDate", stopDate);
		
		String listJson = gridDao.selectPage(MsgBoxDao.class, parm, "getList");
		return listJson;
	}
	
	public MsgBoxModel getDetail(){
		String id = RequestUnit.getString("id");
		MsgBoxModel model1 = new MsgBoxModel();
		model1.setId(id);
		model1.setReaded("1");
		dao.update(model1);
		MsgBoxModel model = dao.getDetail(id);
		return model;
	}
	
	public String getListByuser(){
		String userid = RequestUnit.getString("user_id");
		
		HashMap<String,Object> parm = new HashMap<String, Object>();
		parm.put("userid", userid);
		
		String listJson = gridDao.selectPage(MsgBoxDao.class, parm, "getListByUser");
		return listJson;
	}
	
	public void delete(){
		String id = RequestUnit.getString("id");
		dao.delete(id);
	}
}
